<template>
  <div>
    <div class="user-background-img">
      <!-- <img src="@/assets/images/咖啡.jpg" alt="" /> -->
      <!-- <img :src="imgBase" alt="" /> -->
      <van-uploader
        class="addImg"
        :after-read="methods.onUploadBg"
        style="min-height: 200px"
      >
        <div class="bg-box">
          <img :src="imgBase" alt="用户背景" @error="onLoadError" />
        </div>
      </van-uploader>

      <van-uploader :after-read="methods.onUploadBg" v-if="isHasImg">
        <van-button icon="plus" round color="#999">点击可上传图片</van-button>
      </van-uploader>

      <div class="user-message">
        <div class="user-head">
          <div class="user-pic">
            <van-uploader
              v-model="fileList"
              :deletable="false"
              :max-count="1"
              :after-read="methods.onUploadHeadImg"
            />
          </div>
          <div class="user-title">
            <div class="user-nickname">{{ nickName }}</div>
            <div class="user-motto">{{ motto }}</div>
          </div>
        </div>
        <div class="user-list">
          <van-cell title="个人资料" is-link :to="{ name: 'Personal' }" />
          <van-cell title="我的订单" is-link :to="{ name: 'MyOrder' }" />
          <van-cell title="我的收藏" is-link :to="{ name: 'Collect' }" />
          <van-cell title="地址管理" is-link :to="{ name: 'Address' }" />
          <van-cell title="安全中心" is-link :to="{ name: 'Safe' }" />
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { GET } from '@/api'
import { uploadImage } from '@/utils/upload'
import { UploaderFileListItem } from 'vant'
import { onMounted, ref } from 'vue'

const imgBase = ref<any>(null)
const isHasImg = ref(true)
const nickName = ref('')
const fileList = ref<any[]>([])
const motto = ref('')

const methods = {
  getMyMessage() {
    GET({
      url: '/user/info'
    })
      .then((res) => {
        console.log('res', res)
        imgBase.value = import.meta.env.VITE_APP_PROXY_URL + res.data.userBg
        nickName.value = res.data.username
        isHasImg.value = false
        fileList.value[0] = {
          url: import.meta.env.VITE_APP_PROXY_URL + res.data.userImg
        }
        motto.value = res.data.desc || '这家伙很懒,什么都没有留下'
      })
      .catch((err) => {
        console.error(err)
      })
  },

  onUploadBg(file: UploaderFileListItem | UploaderFileListItem[]) {
    const _file = file as UploaderFileListItem
    // imgBase.value = _file.content
    // const imgType = _file.file?.type.split('/')[1]
    // const serverBase64Img = _file.content?.replace(
    //   /^data:image\/[A-Za-z]+;base64,/,
    //   ''
    // )

    // 上传图片不得大于10MB
    if (_file.file && _file.file.size > 10 * 1024 * 1024) {
      showFailToast('上传图片不得大于10MB')
      return
    }
    if (!_file.file) return
    showLoadingToast({
      duration: 0,
      forbidClick: true,
      message: '图片上传中...'
    })
    uploadImage({
      url: '/user/upload/bg',
      file: _file.file
    })
      .then((res) => {
        imgBase.value = import.meta.env.VITE_APP_PROXY_URL + res.data.userBg
        isHasImg.value = false
      })
      .catch(console.error)
      .finally(closeToast)
  },

  onUploadHeadImg(file: UploaderFileListItem | UploaderFileListItem[]) {
    const _file = file as UploaderFileListItem
    imgBase.value = _file.content
    // 上传图片不得大于10MB
    if (_file.file && _file.file.size > 10 * 1024 * 1024) {
      showFailToast('上传图片不得大于10MB')
      return
    }
    if (!_file.file) return
    showLoadingToast({
      duration: 0,
      forbidClick: true,
      message: '图片上传中...'
    })
    uploadImage({
      url: '/user/upload/avatar',
      file: _file.file
    })
      .then((res) => {
        imgBase.value = import.meta.env.VITE_APP_PROXY_URL + res.data.userImg
      })
      .catch(console.error)
      .finally(closeToast)
  }
}

// 图片加载失败
const onLoadError = () => {
  const defaultUrl = import.meta.env.VITE_APP_PROXY_URL + '/defaultImgs/bg.png'
  if (imgBase.value === defaultUrl) return
  imgBase.value = defaultUrl
}

onMounted(() => {
  methods.getMyMessage()
})
</script>

<style lang="less" scoped>
.user-background-img {
  position: relative;
  min-height: 200px;
  line-height: 200px;
  text-align: center;
  .addImg {
    position: absolute;
    top: 0px;
    left: 0;
    img {
      width: 100%;
    }
  }
  .bg-box {
    width: 100vw;
    min-height: 200px;
  }

  .user-message {
    width: 96%;
    // height: 200px;
    line-height: normal;
    border-radius: 20px;
    background-color: rgba(255, 255, 255, 0.7);
    position: absolute;
    left: 2%;
    top: 80%;
    z-index: 99;
    .user-head {
      height: 100px;
      border-bottom: 1px solid #ddd;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 10px;
      .user-pic {
        width: 80px;
        height: 80px;
        border-radius: 50%;
        overflow: hidden;
        border: 1px solid #ddd;
        :deep(.van-uploader__upload) {
          margin: 0;
        }
        :deep(.van-uploader__preview) {
          margin: 0;
        }
      }
      .user-title {
        height: 80px;
        text-align: left;
        flex: 1;
        padding-left: 20px;
        .user-nickname {
          color: rgb(12, 52, 186);
          font-weight: bold;
          font-size: 20px;
          margin: 10px 0;
        }
        .user-motto {
          color: #555;
          font-size: 14px;
        }
      }
    }
    .user-list {
      text-align: left;
      :deep(.van-cell) {
        background-color: transparent;
      }
    }
  }
}
:deep(.van-uploader) {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
